package cn.wgx.common.base;

import cn.wgx.common.util.ReflectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class IBaseService<M extends IBaseMapper<T>, T extends BaseEntity>
        extends ServiceImpl<M,T> {

    public static List EMPTY_LIST = new ArrayList<>();

    public static Set EMPTY_SET = new HashSet();

    /**
     * 通过反射获取 对象指定field list
     *
     * @param list
     * @param fieldName
     * @param <E>
     * @return
     */
    public <E> List<Integer> getIntegerIdList(List<E> list, String fieldName) {
        ArrayList<Integer> arrayList = Lists.newArrayList();
        if (list != null && !list.isEmpty()) {
            Field field1 = ReflectionUtils.getField(list.get(0).getClass(), fieldName);
            if (field1 == null) {
                return arrayList;
            }
            try {
                field1.setAccessible(true);
                for (E o : list) {
                    Object o1 = field1.get(o);
                    if (o1 instanceof Integer) {
                        arrayList.add((Integer) o1);
                    } else {
                        break;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (!arrayList.isEmpty()) {
            Set set = Sets.newHashSet();
            set.addAll(arrayList);
            arrayList.clear();
            arrayList.addAll(set);
        }
        return arrayList;
    }


}
